//
//  VCOfProduct.m
//  ProjectA_ManualGuest
//
//  Created by dingcong on 16/6/17.
//  Copyright © 2016年 丁聪. All rights reserved.
//

#import "VCOfProduct.h"
#import "NetWorkingHandle.h"
#import "CellOfFourPic.h"
#import "CellOfPicAndPrice.h"
#import "CellOfThreePic.h"
#import "CellOfOneImg.h"
@interface VCOfProduct ()

@property(nonatomic, retain)NSMutableDictionary *dicOfData;
@property(nonatomic, retain)NSMutableArray *arrOfFirstData;
@property(nonatomic, retain)NSMutableArray *arrOfSecondData;
@property(nonatomic, retain)NSMutableArray *arrOfThirdData;
@property(nonatomic, retain)NSMutableArray *arrOfFourthData;
@property(nonatomic, retain)NSMutableArray *arrOfModel;
@end

@implementation VCOfProduct


- (void)dealloc{
    
    [_dicOfData release];
    [_arrOfFirstData release];
    [_arrOfSecondData release];
    [_arrOfThirdData release];
    [_arrOfFourthData release];
    [_arrOfModel release];
    
    
    [super dealloc];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.backgroundColor = [UIColor brownColor];
    
    [self GetData];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView registerClass:[CellOfFourPic class] forCellReuseIdentifier:@"pool1"];
    [self.tableView registerClass:[CellOfPicAndPrice class] forCellReuseIdentifier:@"pool2"];
    [self.tableView registerClass:[CellOfThreePic class] forCellReuseIdentifier:@"pool3"];
    [self.tableView registerClass:[CellOfOneImg class] forCellReuseIdentifier:@"pool4"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)GetData{
    
    NSString *url = @"http://m.shougongke.com/index.php?c=Shiji&vid=18";
    
    NetWorkingHandle *net = [[NetWorkingHandle alloc]init];
    
    
    self.dicOfData = [NSMutableDictionary dictionary];
    self.arrOfFirstData = [NSMutableArray array];
    self.arrOfSecondData = [NSMutableArray array];
    self.arrOfThirdData = [NSMutableArray array];
    self.arrOfFourthData = [NSMutableArray array];
    self.arrOfModel = [NSMutableArray array];
    
    [net netWorkingHandlerGetWithURL:url completion:^(id result, NSData *data, NSURLResponse *response, NSError *error) {
        //        NSLog(@"数据::::%@", result);
        if (result)
        {
            self.dicOfData = [result objectForKey:@"data"];
            ModelOfMaterial *model = [[ModelOfMaterial alloc] init];
            [model setValuesForKeysWithDictionary:_dicOfData];
            [self.arrOfModel addObject:model];
            self.arrOfFirstData = [_dicOfData objectForKey:@"hot"];
            model.arrOfFirst = self.arrOfFirstData;
            self.arrOfSecondData = [_dicOfData objectForKey:@"best"];
            model.arrOfSecond = self.arrOfSecondData;
            self.arrOfThirdData = [_dicOfData objectForKey:@"topicBest"];
            model.arrOfThird = self.arrOfThirdData;
            
            
            [model release];
            
            NSArray *arr = [self.dicOfData objectForKey:@"topic"];
            
            for (NSDictionary *dic in arr)
            {
                ModelOfMaterial *model = [[ModelOfMaterial alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.arrOfFourthData addObject:model];
                [model release];
            }
        }
        
        
        [self.tableView reloadData];
    }];
    [net release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.arrOfFourthData.count + 3;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0)
    {
        return 130  ;
    }
    else if(indexPath.row == 1)
    {
        return 200;
    }
    else if(indexPath.row == 2)
    {
        
        return 150;
        
    }
    else
    {
        return 170;
        
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0)
    {
        CellOfFourPic *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        cell.model = [_arrOfModel lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if(indexPath.row == 1)
    {
        CellOfPicAndPrice *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        //        for (int i = 0; i < self.arrOfSecondData.count; i++)
        //        {
        //            cell.model = self.arrOfSecondData[i];
        //        }
        cell.model = [_arrOfModel lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    else if(indexPath.row == 2)
    {
        CellOfThreePic *cell = [tableView dequeueReusableCellWithIdentifier:@"pool3"];
        cell.model = [_arrOfModel lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    else
    {
        CellOfOneImg *cell = [tableView dequeueReusableCellWithIdentifier:@"pool4"];
        
        
        cell.model = self.arrOfFourthData[indexPath.row - 3];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return cell;
        
    }
    
    
    
    
    
}
@end
